################################################################################
# Require[s] and constants
################################################################################
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

require "logger"

log = Logger.new(STDOUT)

SRC_DIR = "./"
BIN_DIR = SRC_DIR + "bin/"
TMP_DIR = SRC_DIR + "tmp/"

################################################################################
# Default project
################################################################################
task :default => [:zip_project]

################################################################################
# Zip project
################################################################################
SKIP_DIRS = [BIN_DIR, TMP_DIR]
ZIP_NAME = File.join(BIN_DIR + "algos-ruby-#{Time.now.strftime '%Y%m%d'}.zip")

# Requires ruby zip: H2
# XXX how to require gems
desc "Zip the project"
task :zip_project do
  require "zip/zipfilesystem"
  
  Dir.mkdir(BIN_DIR) if ! File.exist?(BIN_DIR)
  File.delete(ZIP_NAME) if File.exists?(ZIP_NAME)

  log.info "Starting compression of file #{ZIP_NAME}..."
  log.info ""

  Zip::ZipFile.open(ZIP_NAME, Zip::ZipFile::CREATE) do |zipfile|
    Dir[SRC_DIR + "**/*"].each do |entry|
      exclude = SKIP_DIRS.inject(false) {|exc, skip| exc or entry.index(skip) == 0}
      unless exclude then
        rel_name = entry.gsub(/^#{Regexp.escape(SRC_DIR)}/, "")

        if File.directory?(entry) then
          zipfile.dir.mkdir(rel_name)
        else
          log.info "Adding entry: #{rel_name}..."

          File.open(entry) do |src_file|
            zipfile.file.open(rel_name, "w") do |dest_file| 
              src_file.each_byte {|b| dest_file.putc(b)}
            end
          end
        end
      end
    end
  end

  log.info ""
  log.info "Compression done!"
end
